package Controller;

import Model.Game;
import Model.Player;
import Model.PlayerAt;
import Model.PlayerDef;
import Model.PlayerGk;
import Model.Team;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;

/**
 * GameData class: control read and wirte of the game data
 * @author Jose Filipe M Santos - 7277502
 * @author Matheus Martins Teixeira - 7277482
 */
public class GameData implements Serializable {

    private Team t1, t2;

    /**
     * Constructor of GameData
     */
    public GameData() {
    }

    /**
     * Rerurns first team loaded from file
     * @return First loaded team
     */
    public Team getLoadedTeam1() {
	return t1;
    }

    /**
     * Returns second team loaded from file
     * @return second loaded team
     */
    public Team getLoadedTeam2() {
	return t2;
    }

    /**
     * Load data from file
     * @param fName file name of file
     */
    public void loadFile(String fName) {
	int activeTeam = 0;

	File file = new File(fName);

	FileReader reader;

	BufferedReader br;

	try {
	    reader = new FileReader(file);
	    br = new BufferedReader(reader);
	    String r;
	    while (br.ready()) {
		r = br.readLine();
		if (r.equals("Goleiro")) {
		    String rName = br.readLine();
		    int rAge = (int) Integer.parseInt(br.readLine());
		    int rHab = (int) Integer.parseInt(br.readLine());
		    int rShirt = (int) Integer.parseInt(br.readLine());
		    int rRef = (int) Integer.parseInt(br.readLine());
		    double rHeight = (double) Double.parseDouble(br.readLine());

		    PlayerGk p = new PlayerGk(rName, rAge, rHab, rRef, rHeight);

		    if (activeTeam == 1) {
			t1.insPlayer(p, rShirt);
		    } else {
			t2.insPlayer(p, rShirt);
		    }

		} else if (r.equals("Defesa")) {
		    String rName = br.readLine();
		    int rAge = (int) Integer.parseInt(br.readLine());
		    int rHab = (int) Integer.parseInt(br.readLine());
		    int rShirt = (int) Integer.parseInt(br.readLine());
		    int rCover = (int) Integer.parseInt(br.readLine());
		    int rChall = (int) Integer.parseInt(br.readLine());
		    int rGoals = (int) Integer.parseInt(br.readLine());

		    PlayerDef p = new PlayerDef(rName, rAge, rHab, rCover, rChall);
		    p.setShirt(rShirt);
		    while (rGoals != p.getGoals()) {
			p.sumGoal();
		    }
		    if (activeTeam == 1) {
			t1.insPlayer(p, rShirt);
		    } else {
			t2.insPlayer(p, rShirt);
		    }

		} else if (r.equals("Ataque")) {
		    String rName = br.readLine();
		    int rAge = (int) Integer.parseInt(br.readLine());
		    int rHab = (int) Integer.parseInt(br.readLine());
		    int rShirt = (int) Integer.parseInt(br.readLine());
		    int rSpeed = (int) Integer.parseInt(br.readLine());
		    int rTech = (int) Integer.parseInt(br.readLine());
		    int rGoals = (int) Integer.parseInt(br.readLine());

		    PlayerAt p = new PlayerAt(rName, rAge, rHab, rSpeed, rTech);
		    p.setShirt(rShirt);
		    while (rGoals != p.getGoals()) {
			p.sumGoal();
		    }
		    if (activeTeam == 1) {
			t1.insPlayer(p, rShirt);
		    } else {
			t2.insPlayer(p, rShirt);
		    }
		} else {
		    activeTeam++;
		    String rName = r;
		    int rWins = (int) Integer.parseInt(br.readLine());
		    int rDraw = (int) Integer.parseInt(br.readLine());
		    int rLost = (int) Integer.parseInt(br.readLine());

		    if (activeTeam == 1) {
			t1 = new Team(rName);
			while (rWins > 0) {
			    t1.sumWin();
			    rWins--;
			}
			while (rDraw > 0) {
			    t1.sumDraw();
			    rDraw--;
			}
			while (rLost > 0) {
			    t1.sumLost();
			    rLost--;
			}
		    } else {
			t2 = new Team(rName);
			while (rWins > 0) {
			    t2.sumWin();
			    rWins--;
			}
			while (rDraw > 0) {
			    t2.sumDraw();
			    rDraw--;
			}
			while (rLost > 0) {
			    t2.sumLost();
			    rLost--;
			}
		    }
		}
	    }
	} catch (FileNotFoundException ex) {
	} catch (IOException ex) {
	} catch (NumberFormatException ex) {
	}
    }

    /**
     * Write game data on file
     * @param fName file name for wirte data
     * @param g Game reference for reach data
     */
    public void writeFile(String fName, Game g) {
	File file = new File(fName);

	FileWriter fw = null;

	Team h, v;
	h = g.getHomeTeam();
	v = g.getVisitorTeam();

	BufferedWriter bw = null;
	try {
	    fw = new FileWriter(file);

	    bw = new BufferedWriter(fw);

	    /* escreve time 1 */
	    bw.write(h.getName());
	    bw.newLine();
	    bw.write(Integer.toString(h.getWins()));
	    bw.newLine();
	    bw.write(Integer.toString(h.getDraws()));
	    bw.newLine();
	    bw.write(Integer.toString(h.getLosts()));
	    bw.newLine();


	    Iterator<Player> p = h.getGkPlayers().iterator();
	    Player aux;

	    while (p.hasNext()) {
		bw.write("Goleiro");
		bw.newLine();
		aux = p.next();
		bw.write(aux.getName());
		bw.newLine();
		bw.write(Integer.toString(aux.getAge()));
		bw.newLine();
		bw.write(Integer.toString(aux.getGHability()));
		bw.newLine();
		bw.write(Integer.toString(aux.getShirt()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerGk) aux).getReflex()));
		bw.newLine();
		bw.write(Double.toString(((PlayerGk) aux).getHeight()));
		bw.newLine();
	    }


	    p = h.getDefensePlayers().iterator();

	    while (p.hasNext()) {
		bw.write("Defesa");
		bw.newLine();
		aux = p.next();
		bw.write(aux.getName());
		bw.newLine();
		bw.write(Integer.toString(aux.getAge()));
		bw.newLine();
		bw.write(Integer.toString(aux.getGHability()));
		bw.newLine();
		bw.write(Integer.toString(aux.getShirt()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerDef) aux).getCover()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerDef) aux).getChallenge()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerDef) aux).getGoals()));
		bw.newLine();
	    }


	    p = h.getAttackPlayers().iterator();

	    while (p.hasNext()) {
		bw.write("Ataque");
		bw.newLine();
		aux = p.next();
		bw.write(aux.getName());
		bw.newLine();
		bw.write(Integer.toString(aux.getAge()));
		bw.newLine();
		bw.write(Integer.toString(aux.getGHability()));
		bw.newLine();
		bw.write(Integer.toString(aux.getShirt()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerAt) aux).getSpeed()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerAt) aux).getTecnique()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerAt) aux).getGoals()));
		bw.newLine();
	    }


	    /* escreve time 2 */
	    bw.write(v.getName());
	    bw.newLine();
	    bw.write(Integer.toString(v.getWins()));
	    bw.newLine();
	    bw.write(Integer.toString(v.getDraws()));
	    bw.newLine();
	    bw.write(Integer.toString(v.getLosts()));
	    bw.newLine();

	    p = v.getGkPlayers().iterator();

	    while (p.hasNext()) {
		bw.write("Goleiro");
		bw.newLine();
		aux = p.next();
		bw.write(aux.getName());
		bw.newLine();
		bw.write(Integer.toString(aux.getAge()));
		bw.newLine();
		bw.write(Integer.toString(aux.getGHability()));
		bw.newLine();
		bw.write(Integer.toString(aux.getShirt()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerGk) aux).getReflex()));
		bw.newLine();
		bw.write(Double.toString(((PlayerGk) aux).getHeight()));
		bw.newLine();
	    }


	    p = v.getDefensePlayers().iterator();

	    while (p.hasNext()) {
		bw.write("Defesa");
		bw.newLine();
		aux = p.next();
		bw.write(aux.getName());
		bw.newLine();
		bw.write(Integer.toString(aux.getAge()));
		bw.newLine();
		bw.write(Integer.toString(aux.getGHability()));
		bw.newLine();
		bw.write(Integer.toString(aux.getShirt()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerDef) aux).getCover()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerDef) aux).getChallenge()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerDef) aux).getGoals()));
		bw.newLine();
	    }


	    p = v.getAttackPlayers().iterator();

	    while (p.hasNext()) {
		bw.write("Ataque");
		bw.newLine();
		aux = p.next();
		bw.write(aux.getName());
		bw.newLine();
		bw.write(Integer.toString(aux.getAge()));
		bw.newLine();
		bw.write(Integer.toString(aux.getGHability()));
		bw.newLine();
		bw.write(Integer.toString(aux.getShirt()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerAt) aux).getSpeed()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerAt) aux).getTecnique()));
		bw.newLine();
		bw.write(Integer.toString(((PlayerAt) aux).getGoals()));
		bw.newLine();
	    }

	    bw.newLine();
	} catch (FileNotFoundException ex) {
	    System.err.println("FileNotFoundException: " + ex.getMessage());
	} catch (IOException ex) {
	    ex.printStackTrace();
	} finally {
	    try {
		if (bw != null) {
		    bw.flush();
		    bw.close();
		}
	    } catch (IOException ex) {
		ex.printStackTrace();
	    }
	}
    }
}
